package com.ygo.feihua.adapter;

import android.content.*;
import android.support.v7.widget.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import com.bumptech.glide.*;
import com.ygo.feihua.*;
import com.ygo.feihua.adapter.*;
import com.yuyh.library.imgsel.*;

import com.ygo.feihua.R;
import java.util.*;
import com.ygo.feihua.util.*;

public class AdapterFt extends RecyclerView.Adapter<AdapterFt.ViewHolder>
{

	private static final int FATIE_ITEM = 0;
	private static final int FATIE_ITEM_ADD = 1;

	private List<FatieMessage> data;
	private Context context;
	private DialogUtils du;
	private int image_position;
	
	public int getImagePosition(){
		return image_position;
	}
	
	@Override
	public int getItemViewType(int position)
	{
		//根据不同position返回不同布局
		if(position==data.size()){
			return FATIE_ITEM_ADD;
		}else{
			return FATIE_ITEM;
		}
	}
	
	public AdapterFt(Context context,List<FatieMessage> data){
		this.context=context;
		this.data=data;
		du=DialogUtils.getdx(context);
	}

	@Override
	public AdapterFt.ViewHolder onCreateViewHolder(ViewGroup p1, int p2)
	{
		View v = null;
		switch(p2){
			case FATIE_ITEM:
				v=LayoutInflater.from(p1.getContext()).inflate(R.layout.item_shequ_fatie, p1,false);
				break;
			case FATIE_ITEM_ADD:
				v=LayoutInflater.from(p1.getContext()).inflate(R.layout.item_shequ_fatie_item, p1,false);
				break;
		}
		ViewHolder viewHolder=new ViewHolder(v,p2);
		// TODO: Implement this method
		return viewHolder;
	}
	boolean isxg=true;

	@Override
	public void onBindViewHolder(AdapterFt.ViewHolder p1, final int position)
	{

		if(getItemViewType(position)==FATIE_ITEM){
			final FatieMessage fm=data.get(position);

			p1.is_tv.setText(fm.getMessage());
			p1.is_tv.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{

						View[] v=du.dialoge(false,"","请输入内容");
						final EditText et=(EditText) v[0];
						Button bt=(Button) v[1];
						et.setText(fm.getMessage());
						bt.setOnClickListener(new OnClickListener(){

								@Override
								public void onClick(View p1)
								{
									String message=et.getText().toString().trim();
									fm.setMessage(message);
									sx();
									du.dis();
									// TODO: Implement this method
								}
							});

						// TODO: Implement this method
					}
				});

			if(fm.getImage_path().equals("")){
				Glide.with(context)
					.asBitmap()
					.load(R.drawable.sc_yl)
					.apply(Gongju.glideOption)
					.into(p1.is_image);
			}else{
				Glide.with(context)
					.asBitmap()
					.load(fm.getImage_path())
					.apply(Gongju.glideOption)
					.into(p1.is_image);
			}
			
			p1.is_image.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						image_position=position;
						ISNav.getInstance().toListActivity(context, Gongju.getPicConfig(1), ShequFabu.REQUEST_GAOJI);

						// TODO: Implement this method
					}
				});
				

			p1.is_additem.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						FatieMessage ftm=new FatieMessage();
						ftm.setImage_path("");
						ftm.setMessage("");
						data.add(position,ftm);
						sx();
						// TODO: Implement this method
					}
				});


			p1.is_remove.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						removeItem(position);
						// TODO: Implement this method
					}
				});
			p1.is_image.setOnLongClickListener(new OnLongClickListener(){

					@Override
					public boolean onLongClick(View p1)
					{
						dialog(position);
						// TODO: Implement this method
						return true;
					}
				});
		}else if(getItemViewType(position)==FATIE_ITEM_ADD){
			p1.is_add.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						FatieMessage ftm=new FatieMessage();
						ftm.setImage_path("");
						ftm.setMessage("");
						data.add(ftm);
						sx();
						// TODO: Implement this method
					}
				});
		}

		// TODO: Implement this method
	}

	@Override
	public int getItemCount()
	{
		// TODO: Implement this method
		return data.size()+1;
	}

	public void sx(){
		isxg=false;
		notifyDataSetChanged();
		isxg=true;
	}

	public class ViewHolder extends RecyclerView.ViewHolder
	{
		ImageView is_image;
		//EditText is_edit;
		TextView is_add,is_remove,is_tv,is_additem;

		public ViewHolder(View v,int type){
			super(v);
			if(type==FATIE_ITEM){
				is_remove=(TextView) v.findViewById(R.id.is_remove);
				is_image = (ImageView) v.findViewById(R.id.is_image);
				is_tv=(TextView) v.findViewById(R.id.is_tv);
				is_additem=(TextView) v.findViewById(R.id.is_additem);
				//is_edit=(EditText) v.findViewById(R.id.is_edit);
			}else if(type==FATIE_ITEM_ADD){
				is_add=(TextView) v.findViewById(R.id.is_add);
			}
		}
	}
	private void removeItem(final int position)
	{
		View[] v=du.dialogt(false,"","确定移除该模块?");
		Button b1,b2;
		b1=(Button) v[0];
		b2=(Button) v[1];

		b1.setText("取消");
		b2.setText("确定");
		b1.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					du.dis();
					// TODO: Implement this method
				}
			});
		b2.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					data.remove(position);
					sx();
					du.dis();
					// TODO: Implement this method
				}
			});

		// TODO: Implement this method
	}

    /* 
     * Dialog对话框提示用户删除操作 
     * position为删除图片位置 
     */  
    protected void dialog(final int position) {  

		View[] v=du.dialogt(false,"","确认移除已添加图片？");
		Button b1,b2;
		b1=(Button) v[0];
		b2=(Button) v[1];

		b1.setText("取消");
		b2.setText("确定");
		b1.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					du.dis();
					// TODO: Implement this method
				}
			});
		b2.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					data.get(position).setImage_path("");
					sx();
					du.dis();
					// TODO: Implement this method
				}
			});

    }  
}
